package com.zthz.douxing.data

import com.zthz.douxing.CityInfo
import com.zthz.douxing.utils.CommonUtil
import com.zthz.douxing.utils.FetchDataUtils
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import org.cyberneko.html.parsers.DOMParser
import org.dom4j.Document
import org.dom4j.Element
import org.dom4j.XPath
import org.dom4j.io.DOMReader
import org.dom4j.xpath.DefaultXPath
import org.jaxen.SimpleNamespaceContext
import org.xml.sax.InputSource
import redis.clients.jedis.Jedis

import javax.xml.stream.events.Namespace


/**
 * PM 2.5每过12个小时查询一次。如果查询失败继续查询。
 * Created by gengjet on 3/30/14.
 */
class PM25FetchJob {

    def redisService
    def commonUtilService

    static triggers = {
        //测试的时候确保没分钟执行一次
        cron name: 'pm25FetchJob', cronExpression: '0 0 0/1 * * ?'
    }

    def group = "fetchData"

    def execute() {

        def http = new HTTPBuilder("http://www.cnpm25.cn/")
        def html = http.request(Method.GET, ContentType.TEXT) { req ->
            uri.path = 'paiming.htm'
            headers.Accept = 'text/html'
            response.success = { resp, reader ->
                println "Got response: ${resp.statusLine}"
                println "Content-Type: ${resp.headers.'Content-Type'}"
                InputSource inputSource = new InputSource(new StringReader(reader.text));
                DOMParser parser = new DOMParser();

                def nameSpaces = ["xmlns": "http://www.w3.org/1999/xhtml"]
                XPath xpath = new DefaultXPath("//xmlns:TABLE[@id='badtable']/TBODY/xmlns:TR");
                xpath.setNamespaceContext(new SimpleNamespaceContext(nameSpaces));


                parser.parse(inputSource);
                org.w3c.dom.Document document = parser.getDocument();
                DOMReader domReader = new DOMReader();
                Document doc = domReader.read(document);
                List<Element> tableNode = xpath.selectNodes(doc)
                List citiesPm25Datas = []
                def currentData = new Date()

                for (Element element : tableNode) {
                    LinkedHashMap<String,String> data = null

                    List<Element> tds = element.elements()
                    def reg = /^(.*)(省|市|县)$/
                    if (tds && tds.size()) {
                        data = FetchDataUtils.getPM25Data(tds)
                        def cityCode =CityInfo.findByNamePinYin(data.get("pinyin"))?.code

                        if(!cityCode){
                            String city = data.get("city")
                            if(city ==~ reg)
                                city = city.substring(0,city.size()-1)

                            cityCode = CityInfo.findByNameCn(city)?.code
                        }

                        if(!cityCode){
                            String provice = data.get("provice")
                            if(provice ==~ reg)
                                provice = provice.substring(0,provice.size()-1)

                            cityCode = CityInfo.findByNameCn(provice)?.code
                        }
                        if(cityCode){
                            def key = FetchDataUtils.getKey([type:FetchDataUtils.TYPE_PM25 ,
                                    date: currentData.format("yyyy-MM-dd"), cityCode: cityCode])
                            def pmData = CommonUtil.getJSONFromateDataFromMap(data)
                            if(pmData){
                                commonUtilService.save2RedisWithExpireTime(key,pmData, 4*60*60)
                            }

                        }
                    }
                }
            }
        }
    }
}

